package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;

/*import org.springframework.security.core.userdetails.User;*/

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    UserService userService;

    //获取当前登录（认证）用户的用户名
    @RequestMapping("/getLoginUsername")
    public Result getLoginUsername() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            String password = user.getPassword();
            Collection<GrantedAuthority> authorities = user.getAuthorities();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        } catch (Exception e) {
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.add(user);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult result = userService.pageQuery(queryPageBean);
        return result;
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            userService.delete(id);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }

        //根据id查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            User user = userService.findById(id);//发送请求
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer id){

        try{
            List<Integer> roleIds=userService.findRoleIdsByUserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,roleIds);
        }catch(Exception ex){
            ex.printStackTrace();
        }

        return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
    }


    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user,Integer[] roleIds){

        try {
            userService.edit(user,roleIds);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }

        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }

}
